﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DataAccess;

namespace iEN.PlatformSet
{
    public partial class UserDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                if (Session["SysAdmin"] != null)
                {
                    // DataTable dt = (DataTable)Session["UserDetailData"];
                    initMenu();
                }
                else
                {
                    Response.Write("<script>alert('請先登入！！');location.href='../Login.aspx';</script>");
                }
            }
        }

        private void initMenu()
        {
            if (Session["SysAdmin"].ToString() != "2")
            {
                Panel1.Visible = false;
                lblMsg.Visible = true;
            }
            
            string selectedAccount;

            if (Request.QueryString.GetValues("Account") == null)
                selectedAccount = Session["LoginAccount"].ToString();
            else
                selectedAccount = Request.QueryString.GetValues("Account")[0];

            DataTable dt = (DataTable)Session["UserDetailData"];
            int i;
            for (i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["_account"].ToString() == selectedAccount)
                    break;
            }

            if (i == dt.Rows.Count)
            {
                // 資料庫無目前登入使用者之帳戶

                return;
            }

            // Fill up each item
            cbEnable.Checked = dt.Rows[i]["_enable"].ToString() == "True" ? true : false;

            tbUsername.Text = dt.Rows[i]["_name"].ToString();

            rblUsertype.Items.FindByValue(dt.Rows[i]["_type"].ToString()).Selected = true;
            
            tbAccount.Text = dt.Rows[i]["_account"].ToString();
            tbAccount.ReadOnly = true;

            // tbPassword.Text = dt.Rows[i]["_password"].ToString();
            tbPassword.Attributes.Add("Value", dt.Rows[i]["_password"].ToString());

            tbPhone.Text = dt.Rows[i]["_phone_office"].ToString();

            tbMobilephone.Text = dt.Rows[i]["_phone_mobile"].ToString();

            tbEmail.Text = dt.Rows[i]["_mail"].ToString();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            bool updateResult;

            if (tbPassword.Text == "")
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "提示訊息", "alert('更新帳號失敗，登入密碼不得為空')", true);
                return;
            }

            updateResult = new DBAccess().updateUserData(cbEnable.Checked.ToString(),
                                           tbUsername.Text,
                                           rblUsertype.SelectedValue,
                                           tbAccount.Text,
                                           tbPassword.Text,
                                           tbPhone.Text,
                                           tbMobilephone.Text,
                                           tbEmail.Text);

            string strAccount = Session["LoginAccount"].ToString();
            string strIP = Session["LoginIP"].ToString();
            string strMemo = "Account：" + tbAccount.Text + "//名稱：" + tbUsername.Text;

            if (updateResult == true)
            {
                new DBAccess().addEventLog(strAccount, strIP, 13, strMemo, "1");
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "提示訊息", "alert('更新帳號資料成功')", true);
                // lShowmsg.Text = "更新帳號資料成功";
                // lShowmsg.Visible = true;
            }
            else
            {
                new DBAccess().addEventLog(strAccount, strIP, 13, strMemo, "0");
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "提示訊息", "alert('更新帳號資料失敗；請稍後再試')", true);
                // lShowmsg.Text = "更新帳號資料失敗；請稍後再試";
                // lShowmsg.Visible = true;
            }
            // this.Page.Response.Redirect("SetUser.aspx");
        }

        protected void backSetUser_Click(object sender, EventArgs e)
        {
            this.Page.Response.Redirect("SetUser.aspx");
        }

        protected void bDeleteUser_Click(object sender, EventArgs e)
        {
            bool result = new DBAccess().deleteUserData( tbAccount.Text );

            string strAccount = Session["LoginAccount"].ToString();
            string strIP = Session["LoginIP"].ToString();
            string strMemo = "Account：" + tbAccount.Text + "//名稱：" + tbUsername.Text;

            if (result == true)
            {
                new DBAccess().addEventLog(strAccount, strIP, 12, strMemo, "1");
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "提示訊息", "alert('刪除帳號資料成功')", true);
                // lShowmsg.Text = "更新帳號資料成功";
                // lShowmsg.Visible = true;
            }
            else
            {
                new DBAccess().addEventLog(strAccount, strIP, 12, strMemo, "0");
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "提示訊息", "alert('刪除帳號資料失敗；請稍後再試')", true);
                // lShowmsg.Text = "更新帳號資料失敗；請稍後再試";
                // lShowmsg.Visible = true;
            }

            this.Page.Response.Redirect("SetUser.aspx");
        }

    }
}